class Solution {
public:
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> result;
        if (!root) return result;
        queue<TreeNode *> path;
        queue<string> qResult;
        path.push(root);
        qResult.push("");
        while (!path.empty()) {
            TreeNode *node = path.front();
            path.pop();
            string curPath = qResult.front();
            qResult.pop();
            curPath.append(to_string(node->val));
            if (!node->left && !node->right)
                result.emplace_back(curPath);
            else {
                curPath.append("->");
                if (node->left) {
                    path.push(node->left);
                    qResult.push(curPath);
                }
                if (node->right) {
                    path.push(node->right);
                    qResult.push(curPath);
                }
            }
        } 
        return result;
    }
};

class Solution2 {
public:
    void findPath(vector<string> &result, string path, TreeNode *node) {
        if (!node) return;
        path += to_string(node->val);
        if (!node->left && !node->right) {
            result.emplace_back(path);
        } else {
            path += "->";
            findPath(result, path, node->left);
            findPath(result, path, node->right);
        }
    }

    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> result;
        string path;
        findPath(result, path, root);
        return result;
    }
};